#include "stdafx.h"
#include "GraphDescriptionReader.h"

GraphDescriptionReader::GraphDescriptionReader()
{
	setDescriptionFileName("");
}

GraphDescriptionReader::GraphDescriptionReader(string descriptionFileName)
{
	setDescriptionFileName(descriptionFileName);
}

string GraphDescriptionReader::getDescriptionFileName()
{
	return descriptionFileName;
}

void GraphDescriptionReader::setDescriptionFileName(string newDescriptionFileName)
{
	descriptionFileName = newDescriptionFileName;
}

void GraphDescriptionReader::readGraphDescriptionFile(GraphDescription *graphDescription)
{
	ifstream descriptionFile;
	string inputString ="";
	try
	{
		descriptionFile.open(descriptionFileName, ifstream::out);
		if(!descriptionFile.fail()) //jesli plik otwarty poprawnie
		{
			getline(descriptionFile,inputString); //wczytaj linie
			graphDescription->setCargoAmount(atoi(inputString.c_str())); //ustaw towar
			while (!descriptionFile.eof()) //wczytuj kolejne linie az znajdziesz koniec pliku
			{
				getline(descriptionFile,inputString);
				Edge edge;
				edge.start = inputString.substr(0,inputString.find(" ")); //ustaw poczatek krawedzi
				inputString = inputString.substr(inputString.find(" ")+1);
				edge.end = inputString.substr(0,inputString.find(" ")); //ustaw koniec krawedzi
				inputString = inputString.substr(inputString.find(" ")+1);
				edge.capacity = atoi(inputString.c_str()); //ustaw przepustowosc krawedzi
				graphDescription->addVertex(edge); //dodaj wierzcholki z nowej krawedzi
			}
		}
		else
		{
			throw new exception(); 
		}
		descriptionFile.close();
	}
	catch(...)
	{
		descriptionFile.close();
		std::cout << "Error::Bad description file!\n";
	}

}

